package com.jlnk.gsondemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.gsondemo.R;
import com.jlnk.gsondemo.entity.OlympicMedal;

import org.json.JSONObject;

import java.util.List;

public class MedalAdapter extends RecyclerView.Adapter<MedalAdapter.MedalViewHolder> {
    private List<OlympicMedal> medalsList;

    public MedalAdapter(List<OlympicMedal> medalsList) {
        this.medalsList = medalsList;
    }

    @NonNull
    @Override
    public MedalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_medal, parent, false);
        return new MedalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MedalViewHolder holder, int position) {
        OlympicMedal medal = medalsList.get(position);
        holder.countryName.setText(medal.getCountryname());
        holder.goldMedals.setText("金牌: " + medal.getGold());
        holder.silverMedals.setText("银牌: " + medal.getSilver());
        holder.bronzeMedals.setText("铜牌: " + medal.getBronze());
        holder.totalMedals.setText("总数: " + medal.getCount());
        holder.rank.setText("排名: " + medal.getRank());
    }

    @Override
    public int getItemCount() {
        return medalsList == null ? 0 : medalsList.size();
    }

    static class MedalViewHolder extends RecyclerView.ViewHolder {
        ImageView flagImageView;
        TextView countryName, goldMedals, silverMedals, bronzeMedals, totalMedals, rank;

        public MedalViewHolder(@NonNull View itemView) {
            super(itemView);
            flagImageView = itemView.findViewById(R.id.imageViewFlag);
            countryName = itemView.findViewById(R.id.textViewCountryName);
            goldMedals = itemView.findViewById(R.id.textViewGold);
            silverMedals = itemView.findViewById(R.id.textViewSilver);
            bronzeMedals = itemView.findViewById(R.id.textViewBronze);
            totalMedals = itemView.findViewById(R.id.textViewTotal);
            rank = itemView.findViewById(R.id.textViewRank);
        }
    }
}